package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.hikvisionutils.HikVisionUtils;
import com.ruoyi.common.utils.hikvisionutils.model.HIKResponseResult;
import com.ruoyi.common.utils.hikvisionutils.model.NodesByParams;
import com.ruoyi.common.utils.hikvisionutils.model.SubRegion;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Regions;
import com.ruoyi.system.service.IRegionsService;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2021-02-23
 */
@RestController
@RequestMapping("/system/regions")
public class RegionsController extends BaseController {
    @Autowired
    private IRegionsService regionsService;
    @Autowired
    IRegionsService regionsServices;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:regions:list')")
    @GetMapping("/list")
    public List<Regions> list(Regions regions) {
//        RegionsService();
        List<Regions> list = regionsService.selectRegionsList(regions);
        return list;
    }

    @GetMapping("/lists")
    public List<Regions> list(String indexCode) {
        List<Regions> list = regionsService.selectRegionsByparentIndexCode(indexCode);
        return list;
    }


    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:regions:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(regionsService.selectRegionsById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:regions:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Regions regions) {
        return toAjax(regionsService.insertRegions(regions));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:regions:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Regions regions) {
        return toAjax(regionsService.updateRegions(regions));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:regions:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(regionsService.deleteRegionsByIds(ids));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:regions:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping("/up")
    public void up(@RequestBody NodesByParams nodesByParams) {
        regionsService.deleteRegions();
        String body = HikVisionUtils.getNodesByParams(nodesByParams).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        List<Map<String, String>> map = (List<Map<String, String>>) hikResponseResult.getData().get("list");
        int b = 0;
        if (map.size() > 0) {
            for (int a = 0; a < map.size(); a++) {
                Regions regions = new Regions();
                regions.setName(map.get(a).get("name"));
                regions.setIndexcode(map.get(a).get("indexCode"));
                regions.setParentindexcode(map.get(a).get("parentIndexCode"));
                regions.setState("0");
                if (regionsService.insertRegions(regions) > 0) {
                    b += 1;
                }
                System.out.println("添加成功++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" + b);
            }
        }
    }
}